package com.shengqi.xinnongren.work.ui.home.presenter;

import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.view.GoodsListView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class GoodsListPresenter extends BasePresenter<GoodsListView> {
    public void getGoodsList(String comClass, int page) {
//        {"storeId":2,"comClass":"b731e50a657011e88990d4bed9ae62f6,0","page":0,"limit":2}
        Map<String, Object> data = new HashMap<>();
        data.put("classId", comClass);
        data.put("start", page);
        data.put("size", 20);
        getView().showLoadingView();
        final JSONObject jsonData = Constants.getJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getGoodsListByClass(requestBody), listHttpRespond -> {
            getView().hideLoadingView();
            if (listHttpRespond.result == 1) {
                getView().onGetListSucceed(listHttpRespond.data);
            }
        });
    }

    public void collectionGoods(String goodsId) {
        Map<String, Object> data = new HashMap<>();
        data.put("comId", goodsId);
        JSONObject aesJsonData = Constants.getAESJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().collectionGoods(requestBody), s -> {
            getView().hideLoadingView();
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1)
                getView().showCollectionResult(Integer.parseInt(httpResult.data));
            else
                getView().onRequestFailed(httpResult.message);
        });
    }
}
